package servlet;

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Objects;

@WebServlet(urlPatterns = "/uploadtest")
public class UploadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            // 1.创建上传文件的对象
            SmartUpload smartUpload = new SmartUpload();
            //2.初始化上
            PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(this,req,resp,null,false,1024,true);
            smartUpload.initialize(pageContext);
            //2.1设置编码
            smartUpload.setCharset("utf-8");
            //3.上传
            smartUpload.upload();
            //4.获取文件信息
            File file = smartUpload.getFiles().getFile(0);
            String fileName= file.getFileName();
            //判别空避免异常
            resp.setContentType("text/html;charset=utf-8");
            if (fileName==null|| Objects.equals(fileName, "")) {
                PrintWriter writer = resp.getWriter();
                writer.println("<script>location.href='index.jsp';alert('文件不可以为空奥，乖乖！');</script>");
            }else {
                System.out.println("name:"+fileName);
                String contentType = file.getContentType();
                //获取文本信息(smartUpload.setAllowedFilesList("jpg,");限制上传文件类型)

                String name =smartUpload.getRequest().getParameter("name");
                System.out.println("name"+name);
                //5.指定上传路径
                String uploadpath ="uploadfiles/"+fileName;
                //6.保存到指定的位置
                file.saveAs(uploadpath,File.SAVEAS_VIRTUAL);
                //7.跳转成功页面
                req.getSession().setAttribute("fileURL",uploadpath);
                req.setAttribute("name",name);
                req.setAttribute("filename",fileName);
                req.getRequestDispatcher("success.jsp").forward(req,resp);
            }
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }

    }
}
